/*
 * @Description: 权限相关方法
 * @Author: smileswlin
 * @LastEditor: smileswlin
 * @Date: 2020-06-18 11:03:38
 * @LastEditTime: 2020-06-27 12:03:30
 */
import { JSEncrypt } from 'js-encrypt'
import { wsCache } from '..'
// import WebStorageCache from 'web-storage-cache'
const tokenKey = `${window.location.host}-GASC-ACCESS-TOKEN`

/**
 * @description: 获取登录token
 * @param {String} name 获取的token key
 * @return:
 */
function getAccessToken(name = tokenKey) {
  return wsCache.get(name)
}

/**
 * @description: 设置登录token
 * @param {String} token 设置的token的值
 * @param {String} name 设置的token key
 * @return:
 */
function setAccessToken(token, name = tokenKey) {
  return wsCache.set(name, token)
}

/**
 * @description: 删除登录token
 * @param {String} name 删除的token key
 * @return:
 */
function removeAccessToken(name = tokenKey) {
  return wsCache.delete(name)
}

/**
 * @description: 加密密码
 * @return: 加密后的密码
 * @param {String} pubKey  公钥
 * @param {String} password  明文的密码
 */
function encrypt(pubKey, password) {
  const encrypt = new JSEncrypt()
  encrypt.setPublicKey(pubKey)
  return encrypt.encrypt(password)
}

export { getAccessToken, setAccessToken, removeAccessToken, encrypt }
